package com.qa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qa.common.Const;
import com.qa.common.ServerResponse;
import com.qa.dao.ArticleMapper;
import com.qa.dao.ArticleVoMapper;
import com.qa.pojo.Article;
import com.qa.pojo.User;
import com.qa.service.IArticleService;
import com.qa.vo.ArticleVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author
 * @Date 2020/3/5 10:44
 * @Desc
 **/
@Service
public class ArticleServiceImpl implements IArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ArticleVoMapper articleVoMapper;

    @Override
    public ServerResponse add(HttpServletRequest request, Article article) {
        String tags = article.getTags().substring(article.getTags().indexOf(",") + 1, article.getTags().length() -1).replace("\"", "");
        User user = (User) request.getSession().getAttribute(Const.CURRENT_USER);
        article.setAuthor(user.getName());
        article.setEmail(user.getEmail());
        if (StringUtils.isBlank(tags)) {
            article.setTags(null);
        } else {
            String tag = "";
            String[] split = tags.split(",");
            for (String s : split) {
                String[] strings = s.split(":");
                tag += strings[0] + ",";
            }
            article.setTags(tag);
        }

        int result = 0;
        article.setStatus(2);
        if (article.getId() == null) {
            result = articleMapper.insert(article);
        } else {
            result = articleMapper.updateByPrimaryKey(article);
        }
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("成功，等待审核！");
        }
        return ServerResponse.createByErrorMessage("失败！");

    }

    @Override
    public PageInfo<Article> getByEmail(String email, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Article> articles = articleMapper.getByEmail(email);
        return new PageInfo<>(articles);
    }

    @Override
    public Article getById(Integer articleId) {
        return articleMapper.selectByPrimaryKey(articleId);
    }

    @Override
    public PageInfo<Article> getByArticleName(String articleName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Article> list = articleMapper.getByArticleName(articleName);
        return new PageInfo<>(list);
    }

    @Override
    public ServerResponse update(Article article) {

       int result = articleMapper.updateStatusById(article.getId(), article.getStatus());
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("成功！");
        }
        return ServerResponse.createByErrorMessage("失败！");
    }

    @Override
    public PageInfo<ArticleVo> getAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ArticleVo> articles = articleVoMapper.getAllByStatus();
        return new PageInfo<>(articles);
    }

    @Override
    public List<Article> getByRandom() {
        return articleMapper.getByRandom();
    }

    @Override
    public ServerResponse deleteById(Integer articleId) {
        int result = articleMapper.deleteByPrimaryKey(articleId);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("成功！");
        }
        return ServerResponse.createByErrorMessage("失败！");
    }

    @Override
    public int getCount(String beginTime, String endTime) {
        return articleMapper.getCount(beginTime, endTime);
    }

    @Override
    public List<Article> getAllByArticleName(String articleName) {
        return articleMapper.getAllByName(articleName);
    }

}
